package com.c.ctgproject.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.c.ctgproject.R
import com.c.ctgproject.base.BaseRecyclerViewAdapter
import com.c.ctgproject.entity.MenuBean
import com.c.ctgproject.ui.service.ServiceLogisticsActivity
import com.c.ctgproject.ui.service.ServiceOrderListActivity
import com.c.ctgproject.utils.MyConstants
import org.jetbrains.anko.startActivity

class MenuAdapter(private val context: Context, private val mList: List<MenuBean>) : BaseRecyclerViewAdapter(context, mList) {
    override fun createViewHolder(parent: ViewGroup): RecyclerView.ViewHolder {
        val view = LayoutInflater.from(context).inflate(R.layout.item_menu_list, parent, false)
        return MyViewHolder(view)
    }

    override fun handlerData(holder: RecyclerView.ViewHolder, position: Int) {
        if (holder is MyViewHolder) {
            val data = mList[position]
            holder.mNameTV.text = data.name
            Glide.with(context).load(data.imageResource).into(holder.mImageView)
            holder.mParentLL.setOnClickListener {
               if (data.name=="物流管理"){
                   context.startActivity<ServiceLogisticsActivity>()
               }else{
                   context.startActivity<ServiceOrderListActivity>(MyConstants.TITLE_NAME to data.name)
               }
            }
            //数字统计
            if (data.count==0){
                holder.mCountTV.visibility=View.GONE
            }else{
                holder.mCountTV.visibility=View.VISIBLE
                //if (data.count<100){
                    holder.mCountTV.text=data.count.toString()
//                }else{
//                    holder.mCountTV.text="99+"
//                }
            }
        }
    }

    inner class MyViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val mNameTV: TextView = view.findViewById(R.id.mNameTV)
        val mImageView: ImageView = view.findViewById(R.id.mImageView)
        val mCountTV: TextView = view.findViewById(R.id.mCountTV)
        val mParentLL: LinearLayout = view.findViewById(R.id.mParentLL)
    }
}